# -*- coding:utf-8 -*-
from pyDes import *
import base64
import time
def des_ecb_encode(source, key):
    if source is None or source == '':
        return ''
    des_obj = triple_des(key, ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
    result = des_obj.encrypt(source.encode('utf-8'))
    return base64.b64encode(result).decode('utf-8')


def des_ecb_decode(source, key):
    if source is None or source == '':
        return ''
    des_obj = triple_des(key, ECB, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
    str=''
    try:
        str = base64.b64decode(source)
    except:
        return str
    result = des_obj.decrypt(str)
    return result.decode('utf-8')

def checkAuth(auth):
    if auth is None or auth == '':
        return False
    decodestr = des_ecb_decode(auth,'YSIa0qCTPVrTL5NW3ulHq4CJ')
    if decodestr is None or decodestr == '':
        return False
    timestamp = int(decodestr)

    return 86400 > int(time.time()) - timestamp

def makeAuth():
    return des_ecb_encode(str(int(time.time())),'YSIa0qCTPVrTL5NW3ulHq4CJ')
if __name__=='__main__':
    src=u'15800100098'
    key="YSIa0qCTPVrTL5NW3ulHq4CJ";
    encrypted  =  des_ecb_encode(src, key)
    print ('encrypted: ', encrypted)
    decrypted = des_ecb_decode(encrypted,key)
    print ('decrypted: ', decrypted)
    print(checkAuth('11'))
